/**
 * www.lifexx.com Inc.
 * Copyright (c) 2014 All Rights Reserved.
 */
package net.mikoo.seals.boss.biz.services.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;

import net.mikoo.seals.boss.biz.mapper.boss.MenuMapper;
import net.mikoo.seals.boss.biz.mapper.boss.RoleMapper;
import net.mikoo.seals.boss.biz.mapper.boss.RolePermitMapper;
import net.mikoo.seals.boss.biz.services.MenuService;
import net.mikoo.seals.boss.common.BaseServices;
import net.mikoo.seals.boss.common.exception.ParametersException;
import net.mikoo.seals.boss.common.helper.MenuHelper;
import net.mikoo.seals.boss.model.Menu;
import net.mikoo.seals.boss.model.MenuQuery;
import net.mikoo.seals.boss.model.Role;
import net.mikoo.seals.boss.model.RolePermit;

/**
 * 
 * @Filename MenuServiceImpl.java
 *
 * @Description
 *
 * @Version 1.0
 *
 * @Author chenzx
 *
 * @Email chenzx1206@163.com
 * 
 * @History
 *          <li>Author: chenzx</li>
 *          <li>Date: 2015骞�1鏈�22鏃�</li>
 *          <li>Version: 1.0</li>
 *          <li>Content: create</li>
 *
 */
@Service
public class MenuServiceImpl extends BaseServices implements MenuService {

	Logger logger = Logger.getLogger(MenuServiceImpl.class);

	@Autowired
	private MenuMapper menuMapper;

	@Autowired
	private RoleMapper roleMapper;

	@Autowired
	private RolePermitMapper rolePermitMapper;

	/**
	 * @param menu
	 * @return
	 * @see cn.shopstart.shop.boss.system.permit.service.MenuService#save(cn.shopstart.shop.boss.system.permit.model.Menu)
	 */
	@Override
	public void save(final Menu menu) {
		bossTransactionTemplate.execute(new TransactionCallback<Menu>() {

			@Override
			public Menu doInTransaction(TransactionStatus status) {

				if (menu == null) {
					throw new ParametersException("menu is null!");
				}

				if (StringUtils.isBlank(menu.getMenuName())) {
					throw new ParametersException("menuName is blank!");
				}
				
				if(StringUtils.isBlank(menu.getIsEnable())) {
					menu.setIsEnable("N");
				}
				
				if (menu.getSequence() == null) {
					menu.setSequence(0);
				}

				menuMapper.insert(menu);

				if (menu.getIsEnable().equalsIgnoreCase("Y")) {
					insertRolePermit(menu);
				}
				return menu;

			}
		});
	}

	private void insertRolePermit(Menu menu) {
		List<Role> roles = roleMapper.selectAll();
		if (roles != null && roles.size() > 0) {
			for (Role role : roles) {
				if (role.getEnabled().equalsIgnoreCase("Y")) {
					RolePermit rolePermit = new RolePermit();
					rolePermit.setRoleId(role.getRoleId());
					rolePermit.setMenuId(menu.getMenuId());
					rolePermit.setIsEnabled("Y");
					rolePermitMapper.insert(rolePermit);
				}
			}
		}
	}

	/**
	 * @param menu
	 * @return
	 * @see cn.shopstart.shop.boss.system.permit.service.MenuService#modifyByPrimaryKey(cn.shopstart.shop.boss.system.permit.model.Menu)
	 */
	@Override
	public void modifyByPrimaryKey(Menu menu) {

		if (menu == null) {
			throw new ParametersException("menu is null!");
		}
		if (menu.getMenuId() == null) {
			throw new ParametersException("MenuId is null!");
		}
		
		
		if (StringUtils.isBlank(menu.getMenuName())) {
			throw new ParametersException("请输入菜单名称");
		}
		
		if(StringUtils.isBlank(menu.getIsEnable())) {
			throw new ParametersException("请选择是否启用");
		}
		
		if(menu.getSequence() == null) {
			throw new ParametersException("请输入菜单顺序");
		}
		
		menuMapper.updateByPrimaryKey(menu);

	}

	/**
	 * @param menuId
	 * @return
	 * @see cn.shopstart.shop.boss.system.permit.service.MenuService#loadByPrimaryKey(java.lang.Long)
	 */
	@Override
	public Menu loadByPrimaryKey(Long menuId) {
		if (menuId == null) {
			throw new ParametersException("menuId is null!");
		}

		Menu menu = menuMapper.selectByPrimaryKey(menuId);
		// 鎵撳嵃鎵ц閿欒鏃ュ織
		return menu;
	}

	/**
	 * @param menuId
	 * @return
	 * @see cn.shopstart.shop.boss.system.permit.service.MenuService#deleteByPrimaryKey(java.lang.Long)
	 */
	@Override
	public void deleteByPrimaryKey(Long menuId) {

		if (menuId == null) {
			throw new ParametersException("menuId is null!");
		}
		// 濡傛灉鑿滃崟鏄彲鐢ㄧ殑锛屽悓鏃跺垹闄よ鑹叉潈闄愯〃鐨勫搴旀暟鎹�
		Menu menuOld = menuMapper.selectByPrimaryKey(menuId);
		if (menuOld != null && menuOld.getIsEnable().equalsIgnoreCase("Y")) {
			rolePermitMapper.deleteByMenuId(menuId);
		}

		menuMapper.deleteByPrimaryKey(menuId);

	}

	/**
	 * @param parentId
	 * @return
	 * @see cn.shopstart.shop.boss.system.permit.service.MenuService#loadByParentId(java.lang.Long)
	 */
	@Override
	public List<Menu> loadByParentId(Long parentId) {

		if (parentId == null) {
			parentId = 0L;
		}
		List<Menu> menus = menuMapper.selectByParentId(parentId);
		return menus;
	}

	/**
	 * @return
	 * @see cn.shopstart.shop.boss.system.permit.service.MenuService#loadAsTree()
	 */
	@Override
	public List<Menu> loadAsTree() {

		List<Menu> childs = menuMapper.selectAll();
		List<Menu> menus = MenuHelper.convertToTree(childs, 0L,0);

		return menus;
	}

	@Override
	public List<Menu> getListByQuery(MenuQuery query) {
		List<Menu> menus = menuMapper.selectListByQuery(query);
		return menus;
	}

}
